<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">

            <el-form-item label="二维码类型" prop="type">
                <el-input v-model="formData.type" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="场景值ID" prop="sceneId">
                <el-input v-model="formData.sceneId" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="票据" prop="ticket">
                <el-input v-model="formData.ticket" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="uid" prop="uid">
                <el-input v-model="formData.uid" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="二维码链接" prop="url">
                <el-input v-model="formData.url" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="过期时间" prop="expirationTime">
                <el-input v-model="formData.expirationTime" placeholder="请输入"/>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" style="margin-right: 20px;">取 消</el-button>
            <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {QrCodeApi} from '@/api/user/QrCodeApi'

export default {
    name: 'UserQrCodeUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: '',
                type: '',
                sceneId: '',
                ticket: '',
                uid: '',
                url: '',
                expirationTime: '',
            },
            rules: {

                type: [
                    {required: true, message: '请输入二维码类型', trigger: 'blur'},
                    {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
                ],


                sceneId: [
                    {required: true, message: '请输入场景值ID', trigger: 'blur'},
                    {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
                ],


                ticket: [
                    {required: true, message: '请输入票据', trigger: 'blur'},
                    {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
                ],


                uid: [
                    {required: true, message: '请输入uid', trigger: 'blur'},
                    {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
                ],


                url: [
                    {required: true, message: '请输入二维码链接', trigger: 'blur'},
                    {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
                ],


                expirationTime: [
                    {required: true, message: '请输入过期时间', trigger: 'blur'},
                    {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
                ],


            },
            dialogVisible: false
        }
    },
    created() {
    },
    methods: {
        // 显示组件
        async showUpdate(id) {
            const that = this

            that.dialogVisible = true
            if (that.$refs['formRef'] !== undefined) {
                that.$refs['formRef'].resetFields()
            }


            that.$nextTick(() => {
                if (id > 0) {
                    QrCodeApi.load(id).then((response) => {
                        that.formData = Object.assign({}, response)
                    })
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (!valid) return false

                if (that.formData.id) {
                    QrCodeApi.edit(that.formData).then(() => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                } else {
                    QrCodeApi.create(that.formData).then(() => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                }
            })
        }
    }
}
</script>


